//
//  LSZLyricBooksPopup.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/8/4.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZLyricBooksPopup.h"

 
@interface LSZLyricBooksPopup ()
@property(nonatomic,strong) UITableView * tableViewList;
@property(nonatomic,strong) UIView *topView;
@property(nonatomic,strong) UIView *viewLowWhite;



@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * arrayMutableArray;
@end
@implementation LSZLyricBooksPopup
- (void)initWithKeyword:(NSString *)Keyword;
{
    self.page = 1;
    [self getDataDicWithKeyword:Keyword :self.page];
}
-(void)getDataDicWithKeyword:(NSString *)Keyword :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic thePopoverOfPoetryAndMusic:Keyword page:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOk, NSString * string, LSZLyricBooksManager * mg) {
        if (isOk == YES) {
            if (page== 1) {
                [weakSelf.arrayMutableArray removeAllObjects];
                [weakSelf.arrayMutableArray addObjectsFromArray:mg.mutMgArray];
                if (weakSelf.arrayMutableArray.count == 0) {
                    NSLog(@"没有数据");
                    weakSelf.tableViewList.mj_footer.hidden = YES;
                    LSZSpecificEmptyData * vc = [[LSZSpecificEmptyData alloc]init];
                    [vc showWithView:weakSelf.tableViewList];
                }else{
                    [weakSelf.tableViewList reloadData];
                }
            }else{
                [weakSelf.arrayMutableArray addObjectsFromArray:mg.mutMgArray];
                [weakSelf.tableViewList reloadData];
            }
        }else{

        }
    }];
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.5];
        __weak typeof(self) weakSelf = self;
        weakSelf.arrayMutableArray = [[NSMutableArray alloc]init];
         /*
         阴影区域点击
         */
        weakSelf.topView = [[UIView alloc]init];
        weakSelf.topView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        weakSelf.topView.backgroundColor = [UIColor clearColor];
        [weakSelf addSubview:weakSelf.topView];

        [weakSelf.topView tapGesture:^(UIGestureRecognizer *ges) {
            [weakSelf hiddenMBhiddenView];
        }];

        weakSelf.viewLowWhite = [[UIView alloc]init];
        weakSelf.viewLowWhite.frame = CGRectMake(0,weakSelf.topView.frame.size.height-300, ScreenWidth,300);
        weakSelf.viewLowWhite.backgroundColor = [UIColor whiteColor];
        [weakSelf.viewLowWhite.layer setMasksToBounds:YES];
        [weakSelf.viewLowWhite.layer setCornerRadius:8];
        [weakSelf addSubview:weakSelf.viewLowWhite];

        UILabel * labelTitle = [[UILabel alloc]init];
        labelTitle.textColor = redUIColor;
        labelTitle.text = @"操作列表";
        labelTitle.font = FONT_18;
        labelTitle.textAlignment = NSTextAlignmentCenter;
        [weakSelf.viewLowWhite addSubview:labelTitle];
        if (ScreenWidth == 320) {
            //5
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 40);
        }else if(ScreenWidth == 375){
            // 6 7
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 45);
        }else {
            //plus
            labelTitle.frame = CGRectMake(0, 0, ScreenWidth, 53);
        }
        UIView * viewTop = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(labelTitle.frame), ScreenWidth, 0.3)];
        viewTop.backgroundColor = divider;//RGBColor(186, 186, 186)
        [weakSelf.viewLowWhite addSubview:viewTop];

        weakSelf.tableViewList = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(viewTop.frame), ScreenWidth,weakSelf.viewLowWhite.frame.size.height-CGRectGetMaxY(viewTop.frame)) style:UITableViewStyleGrouped];
        weakSelf.tableViewList.dataSource = self;
        weakSelf.tableViewList.delegate = self;
        weakSelf.tableViewList.separatorColor = divider;
        weakSelf.tableViewList.backgroundColor = [UIColor whiteColor];
        weakSelf.tableViewList.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        weakSelf.tableViewList.estimatedRowHeight =0;
        weakSelf.tableViewList.estimatedSectionFooterHeight =0;
        weakSelf.tableViewList.estimatedSectionHeaderHeight =0;
        [weakSelf.tableViewList registerClass:[LSZInformationChooseTableViewCell class] forCellReuseIdentifier:@"LSZInformationChooseTableViewCell"];
        [weakSelf.viewLowWhite addSubview:weakSelf.tableViewList];
    }
    return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 40;
    }else if(ScreenWidth == 375){
        // 6 7
        return 45;
    }
    else{
        //plus
        return 53;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayMutableArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if (self.arrayMutableArray.count >indexPath.row) {
        LSZLyricBooksMg * mg = nil;
        mg = self.arrayMutableArray[indexPath.row];
        cell.textLabel.text = [NSString stringWithFormat:@"%@--%@",mg.name,mg.album];
        cell.textLabel.textColor = titleColorLSZ;
        cell.textLabel.font = FONT_13;
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@(indexPath.row),@"row",self.arrayMutableArray,@"array", nil];
    NSNotification * notification =[NSNotification notificationWithName:@"ClickSpecificSearch" object:nil userInfo:dic];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ClickSpecificSearch" object:nil];
    [self hiddenMBhiddenView];

}
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
-(void)hiddenMBhiddenView
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeFromSuperview];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeFromSuperview];
}
@end
